package com.lz.comm.binding

import android.view.View
import android.widget.ImageView
import android.widget.RadioGroup
import androidx.databinding.BindingAdapter
import androidx.databinding.adapters.ListenerUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import androidx.viewpager2.widget.getRecyclerView
import com.lz.comm.R
import com.lz.comm.widget.fragment.FragmentLayout
import com.google.android.material.tabs.TabLayout

/**
 *  author: you : 2020/5/5
 */
class ViewBindings {

    interface OnScrollStateChanged {
        fun onScrollStateChanged(rv: RecyclerView, newState: Int)
    }

    interface OnScrolled {
        fun onScrolled(rv: RecyclerView, dx: Int, dy: Int)
    }

    companion object {
        /**
         * StaggeredGridLayoutManager时的状态
         * @param rv
         * @param handlingGap
         */
        @BindingAdapter("handlingGap")
        @JvmStatic
        fun handleGap(rv: RecyclerView, handlingGap: Boolean) {
            if (rv.layoutManager is StaggeredGridLayoutManager) {
                val sglm = rv.layoutManager as StaggeredGridLayoutManager
                val gapStrategy =
                    if (handlingGap) StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS else StaggeredGridLayoutManager.GAP_HANDLING_NONE
                sglm.gapStrategy = gapStrategy
            }
        }

        @BindingAdapter("itemDecoration")
        @JvmStatic
        fun addItemDecoration(rv: RecyclerView, decor: ItemDecoration?) {
            val oldValue = ListenerUtil.trackListener(rv, decor, R.id.comm_recyclerItemDecoration)
            if (oldValue != null) {
                rv.removeItemDecoration(oldValue)
            }
            if (decor != null) {
                rv.addItemDecoration(decor)
            }
        }

        @BindingAdapter("itemViewCacheSize")
        @JvmStatic
        fun setItemViewCacheSize(rv: RecyclerView, cacheSize: Int) = rv.setItemViewCacheSize(cacheSize)

        @BindingAdapter("hasFixedSize")
        @JvmStatic
        fun setHasFixedSize(rv: RecyclerView, hasFixedSize: Boolean) = rv.setHasFixedSize(hasFixedSize)

        @BindingAdapter(value = ["onScrollStateChanged", "onScrolled"], requireAll = false)
        @JvmStatic
        fun addScrollListener(rv: RecyclerView, stateChanged: OnScrollStateChanged?, scrolled: OnScrolled?) {
            val newValue = if (stateChanged == null && scrolled == null) null else object : RecyclerView.OnScrollListener() {
                override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                    stateChanged?.onScrollStateChanged(recyclerView, newState)
                }

                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    scrolled?.onScrolled(recyclerView, dx, dy)
                }
            }
            val oldValue = ListenerUtil.trackListener(rv, newValue, R.id.comm_recyclerScroller)
            if (oldValue != null) {
                rv.removeOnScrollListener(oldValue)
            }
            if (newValue != null) {
                rv.addOnScrollListener(newValue)
            }
        }

        @BindingAdapter("level")
        @JvmStatic
        fun setImageViewLevel(iv: ImageView, level: Int) = iv.setImageLevel(level)

        @BindingAdapter("selected")
        @JvmStatic
        fun setViewSelected(view: View, selected: Boolean) {
            view.isSelected = selected
        }

        @BindingAdapter("enabled")
        @JvmStatic
        fun setEnabled(view: View, enabled: Boolean) {
            view.isEnabled = enabled
        }

        @BindingAdapter("checkRadio")
        @JvmStatic
        fun check(group: RadioGroup, radioButtonId: Int) {
            group.check(radioButtonId)
        }

        @BindingAdapter("setUpTab")
        @JvmStatic
        fun setupWithViewPager(vp: ViewPager, tl: TabLayout) = tl.setupWithViewPager(vp)

        @BindingAdapter(value = ["currentItem", "smoothScroll"], requireAll = false)
        fun setCurrentItem(vp: ViewPager, item: Int, smoothScroll: Boolean = false) = vp.setCurrentItem(item, smoothScroll)

        @BindingAdapter(value = ["currentItem", "smoothScroll"], requireAll = false)
        @JvmStatic
        fun setCurrentItem(vp: ViewPager2, item: Int, smoothScroll: Boolean = false) = vp.setCurrentItem(item, smoothScroll)

        @BindingAdapter("cacheSize")
        @JvmStatic
        fun setItemViewCacheSize(vp: ViewPager2, cacheSize: Int) = vp.getRecyclerView().setItemViewCacheSize(cacheSize)

        @BindingAdapter("currentItem")
        @JvmStatic
        fun setCurrentItem(vp: ViewPager2, item: Int) = vp.setCurrentItem(item, false)

        @BindingAdapter("currentItem")
        @JvmStatic
        fun setCurrentItem(fragmentLayout: FragmentLayout, current: Int) {
            fragmentLayout.setCurrentItem(current)
        }
    }

}